package com.phone580.app.updateapp;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.Toast;

import com.phone580.app.AppUpgradeTask;
import com.phone580.app.BaseActivity;
import com.phone580.app.R;
import com.phone580.app.BaseTask.TaskCallback;
import com.phone580.app.entity.AppInfoEntity;
import com.phone580.app.untilty.CommonUtility;

/**
 * 
 * @author matthew
 *
 */
public class UpdateAppActivity extends BaseActivity implements TaskCallback{
	private ListView lvAppList;
	private ProgressBar pbLoad;
	private UpdateAppAdapter adapter;
//	private Map<String, AppInfoEntity> cache = new HashMap<String, AppInfoEntity>();
//	private Handler handler = new Handler() {
//		public void handleMessage(Message msg) {
//			if (AppUpgradeTask.appUpgrade == null) {
//				AppUpgradeTask task = new AppUpgradeTask(UpdateAppActivity.this);
//				task.setTaskCallback(UpdateAppActivity.this);
//				task.execute();
//			} else {
//				pbLoad.setVisibility(View.GONE);
//				adapter.setData(AppUpgradeTask.appUpgrade.getApps());
//				lvAppList.setAdapter(adapter);
//			}
//		};
//	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.update_app_layout);
		initHeader("软件升级");
		lvAppList = (ListView) findViewById(R.id.update_app_list);
		pbLoad = (ProgressBar) findViewById(R.id.update_app_load_pb);
		refresh();
	}
	
	public void refresh() {
		pbLoad.setVisibility(View.VISIBLE);
//		Thread t = new WorkerThread();
		adapter = new UpdateAppAdapter(this);
//		adapter.setData(new ArrayList<AppInfoEntity>());
//		t.start();
		AppUpgradeTask task = new AppUpgradeTask(UpdateAppActivity.this);
		task.setTaskCallback(UpdateAppActivity.this);
		task.execute();
	}
	
//	@Override
//	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//		super.onActivityResult(requestCode, resultCode, data);
//		refresh();
//	}
	
	class WorkerThread extends Thread {
		@Override
		public void run() {
//			List<PackageInfo> apps = new ArrayList<PackageInfo>();
//			PackageManager pManager = getPackageManager();
//			// 获取手机内所有应用
//			List<PackageInfo> paklist = pManager.getInstalledPackages(0);
//			for (int i = 0; i < paklist.size(); i++) {
//				PackageInfo pak = (PackageInfo) paklist.get(i);
//				// 判断是否为非系统预装的应用程序
//				if ((pak.applicationInfo.flags & ApplicationInfo.FLAG_SYSTEM) <= 0) {
//					// customs applications
//					apps.add(pak);
//				}
//			}
//
//			data = new ArrayList<AppInfoEntity>();
//			for (PackageInfo pkg : apps) {
//				String label = pManager.getApplicationLabel(pkg.applicationInfo)
//						.toString();
//				String pkgName = pkg.packageName;
//				String version = pkg.versionName;
//				Drawable icon = pManager.getApplicationIcon(pkg.applicationInfo);
//				String srcPath = pkg.applicationInfo.sourceDir;
//				File file = new File(srcPath);
//				long size = file.length();
//				AppInfoEntity info = new AppInfoEntity();
//				info.setAppLabel(label);
//				info.setIcon(icon);
//				info.setPkgName(pkgName);
//				info.setVersion(version);
//				info.setSize(size);
//				data.add(info);
//			}
//			 List<AppInfoEntity> data = CommonUtility.getMyAppList(UpdateAppActivity.this);
//			 for (AppInfoEntity entity : data) {
//				 cache.put(entity.getPkgName(), entity);
//			 }
//			 data.clear();
//			 data = null;
//			handler.sendEmptyMessage(0);
		}
	}

	@Override
	public void execute(Object result) {
		pbLoad.setVisibility(View.GONE);
		if (AppUpgradeTask.appUpgrade == null) {
			Toast.makeText(this, "检查更新失败，请稍后再试", Toast.LENGTH_LONG).show();
			finish();
		} else {
			pbLoad.setVisibility(View.GONE);
			if (AppUpgradeTask.appUpgrade.getApps() != null) {
				adapter.setData(AppUpgradeTask.appUpgrade.getApps());
				lvAppList.setAdapter(adapter);
			}
		}
		
	}
}
